<?php
/**
 * 权限管理
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/2/26
 * Time: 11:34
 */

namespace app\admin\controller;

use app\common\controller\AdminBase;
use app\common\model\Access as AccessModel;
use app\common\model\BoolStatus;
use app\admin\validate\Access as AccessValidate;

class Access extends AdminBase
{
    /**
     * 列表
     * @return mixed
     */
    public function index()
    {
        $accessModel = new AccessModel();
        $list        = $accessModel->column('id, title, urls, status, updated_time, created_time, pid');
        $this->assign('list', generateTree($list));

        $status = BoolStatus::getLables();
        $this->assign('status', $status);
        return $this->fetch('access/index');
    }

    /**
     * 设置
     * @return mixed
     */
    public function set()
    {
        $id          = $this->request->param('id', 0, 'int');
        $accessModel = new AccessModel();
        if ($this->request->isPost()) {
            $title  = $this->request->param('title');
            $urls   = $this->request->param('urls');
            $status = $this->request->param('status');
            $pid    = $this->request->param('pid');
            //验证数据
            $validate = new AccessValidate();
            if (!$validate->check(['title' => $title, 'urls' => $urls, 'status' => $status])) {
                $this->error($validate->getError());
            }
            $currentTime = time();
            if (!$id) {
                //新增
                $count = $accessModel->where('title', '=', $title)->count();
                if ($count) {
                    $this->error('该权限名称已存在，请输入其他的权限名称~~');
                }
                $accessModel->created_time = $currentTime;

            } else {
                //编辑
                $count = $accessModel->where('title', '=', $title)->whereNotIn('id', $id)->count();
                if ($count) {
                    $this->error('该权限名称已存在，请输入其他的权限名称~~');
                }
                $accessModel = $accessModel->find($id);
            }
            $accessModel->pid          = $pid;
            $accessModel->urls         = $urls;
            $accessModel->updated_time = $currentTime;
            $accessModel->title        = $title;
            $accessModel->status       = $status;

            $result = $accessModel->save();
            if ($result) {
                $this->success('操作成功~~', 'access/index');
            } else {
                $this->error('操作失败~~');
            }
        }
        $access = null;
        if ($id) {
            $access = $accessModel->find($id);
        }
        $this->assign('access', $access);

        $pids = $accessModel->where('pid', '=', 0)->whereNotIn('id', $id)->field('id, title')->select();
        $this->assign('pids', $pids);

        $status = BoolStatus::getLables();
        $this->assign('status', $status);
        return $this->fetch('access/set');
    }
}